declare namespace g = "ddi:group:3_1";
declare namespace dc="ddi:dcelements:3_1";
declare namespace d="ddi:datacollection:3_1"; 
declare namespace dc2="http://purl.org/dc/elements/1.1/"; 
declare namespace s="ddi:studyunit:3_1"; 
declare namespace c="ddi:conceptualcomponent:3_1";
declare namespace r="ddi:reusable:3_1"; 
declare namespace a="ddi:archive:3_1"; 
declare namespace ddi="ddi:instance:3_1"; 
declare namespace l="ddi:logicalproduct:3_1";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace transform="http://exist-db.org/xquery/transform";

(: study-id as parameter :)
let $id := request:get-parameter("id", ("SND0157"))
let $lang := request:get-parameter("lang", "en")

let $collection := collection('/db/ddi/data/ddi3/internal')

let $result := $collection/ddi:DDIInstance/s:StudyUnit[@id=$id]/r:Citation/dc:DCElements/*[@xml:lang=$lang or not(@xml:lang)]

let $params := <parameters></parameters>

let $dc := <dc2:dc xmlns="http://purl.org/dc/elements/1.1/">{$result}</dc2:dc>
    
let $marc := transform:transform($dc, xs:anyURI("xmldb:exist:///db/ddi/xsl/util/DC2MARC21slim.xsl"), $params)
    
return $marc